package com.wg.net.server.tcp;

import com.wg.net.session.TcpSession;

import java.util.concurrent.ConcurrentHashMap;

public class TcpSessionCache {
    private final ConcurrentHashMap<String, TcpSession> items = new ConcurrentHashMap<>();

    public static TcpSessionCache getInstance() {
        return Singleton.INSTANCE.getManager();
    }

    public void addPeer(TcpSession peer) {
        this.items.put(peer.getPeerId(), peer);
    }

    public TcpSession get(String peerId) {
        return this.items.get(peerId);
    }

    public void remove(String peerId) {
        this.items.remove(peerId);
    }

    private enum Singleton {
        INSTANCE;
        TcpSessionCache manager;

        Singleton() {
            this.manager = new TcpSessionCache();
        }

        public TcpSessionCache getManager() {
            return manager;
        }
    }
}
